package com.ken.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ken.dto.OrdersCancelDTO;
import com.ken.dto.OrdersPageQueryDTO;
import com.ken.dto.OrdersRejectionDTO;
import com.ken.entity.Orders;
import com.ken.result.PageResult;
import com.ken.result.Result;
import com.ken.service.OrderService;
import com.ken.vo.OrderStatisticsVO;
import com.ken.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.ken.entity.Orders.*;

@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Slf4j
@Api(tags ="admin端订单接口")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/details/{id}")
    @ApiOperation("根据id查询订单详情")
    public Result<OrderVO> findOrdersDetailById(@PathVariable Long id){
        log.info("查询id为{}的订单",id);
        OrderVO orderVo = orderService.findById(id);
        return Result.success(orderVo);
    }

    @GetMapping("/conditionSearch")
    @ApiOperation("订单查询")
    public Result<PageResult> queryOrdersPageQuery(OrdersPageQueryDTO ordersPageQueryDTO){
        Page<Orders> page = orderService.lambdaQuery()
                .ge(Orders::getOrderTime, ordersPageQueryDTO.getBeginTime())
                .le(Orders::getOrderTime, ordersPageQueryDTO.getEndTime())
                .like(ordersPageQueryDTO.getPhone() != null, Orders::getPhone, ordersPageQueryDTO.getPhone())
                .like(ordersPageQueryDTO.getStatus() != null, Orders::getStatus, ordersPageQueryDTO.getStatus())
                .like(ordersPageQueryDTO.getNumber()!=null,Orders::getNumber,ordersPageQueryDTO.getNumber())
                .page(new Page<>(ordersPageQueryDTO.getPage(), ordersPageQueryDTO.getPageSize()));
        return Result.success(new PageResult(page.getTotal(),page.getRecords()));
    }

    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数量统计")
    public Result<OrderStatisticsVO> orderStatistics(){
        // 查询每个状态对应的数量
        long toBeConfirmedCount = orderService.query().eq("status", Orders.TO_BE_CONFIRMED).count();
        long confirmedCount = orderService.query().eq("status", CONFIRMED).count();
        long deliveryInProgressCount = orderService.query().eq("status", Orders.DELIVERY_IN_PROGRESS).count();
        // 构建 VO 对象
        OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO();
        orderStatisticsVO.setToBeConfirmed((int) toBeConfirmedCount);
        orderStatisticsVO.setConfirmed((int) confirmedCount);
        orderStatisticsVO.setDeliveryInProgress((int) deliveryInProgressCount);
        // 返回包装结果
        return Result.success(orderStatisticsVO);
    }

    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public Result<String> cancelOrder(@RequestBody OrdersCancelDTO orderCancelDTO){
        orderService
                .update()
                .eq("id",orderCancelDTO.getId())
                .set(orderCancelDTO.getCancelReason()!=null,"cancel_reason",orderCancelDTO.getCancelReason())
                .set("status",CANCELLED);
        return Result.success();
    }

    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result<String> completeOrder(@PathVariable Long id){
        orderService
                .update()
                .eq("id",id)
                .set("status",COMPLETED);
        return Result.success();
    }

    @PutMapping("/rejection")
    @ApiOperation("解决订单")
    public Result<String> rejectionOrder(@RequestBody OrdersRejectionDTO ordersRejectionDTO){
        orderService
                .update()
                .eq("id",ordersRejectionDTO.getId())
                .set(ordersRejectionDTO.getRejectionReason()!=null,"rejection_reason",ordersRejectionDTO.getRejectionReason())
                .set("status",CANCELLED);
        return Result.success();
    }

    @PutMapping("/confirm")
    @ApiOperation("接受订单")
    public Result<String> confirmOrder(@RequestBody Integer id){
        orderService
                .update()
                .eq("id",id)
                .set("status",CONFIRMED);
        return Result.success();
    }

    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单")
    public Result<String> deliveryOrder(@PathVariable String id){
        orderService
                .update()
                .eq("id",id)
                .set("status",DELIVERY_IN_PROGRESS);
        return Result.success();
    }


}
